package com.ly.lypopupwindowdemo.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.androidkun.xtablayout.XTabLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lypopupwindowdemo.BaseTitle;
import com.ly.lypopupwindowdemo.MessageEdit;
import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.TXMessageEdit;
import com.ly.lypopupwindowdemo.adapter.FragmentAdapter;
import com.ly.lypopupwindowdemo.fragment.MessageCenterGGFragment;
import com.ly.lypopupwindowdemo.fragment.MessageCenterTXFragment;

import java.util.ArrayList;
import java.util.List;

public class MessageCenterActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {

    @ViewInject(R.id.viewpager)
    private ViewPager viewPager;
    List<Fragment> fragments = new ArrayList<>();

    List<String> titles = new ArrayList<>();

    @ViewInject(R.id.title)
    private BaseTitle baseTitle;

    int currentPage;

    @ViewInject(R.id.id_bottom_ll)
    private LinearLayout bottom_ll;

    @ViewInject(R.id.id_quanxuan_ll)
    private LinearLayout quanxuan_ll;
    @ViewInject(R.id.id_yidu_ll)
    private LinearLayout yidu_ll;
    @ViewInject(R.id.id_delete_ll)
    private LinearLayout delete_ll;

    @ViewInject(R.id.id_quanxuan_check)
    private CheckBox quanxuan_check;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_center);

        ViewUtils.inject(this);


        initViewPager();
        setListeners();
    }

    private void setListeners() {

        baseTitle.setRightOnClickLinstner(new BaseTitle.RightOnClick() {
            @Override
            public void click() {

                if (baseTitle.getRightStr().equals("编辑")) {
                    baseTitle.setRightStr("取消");
                    bottom_ll.setVisibility(View.VISIBLE);

                    if (messageEdit != null && currentPage == 0) {
                        messageEdit.onEdit();
                    }

                    if (messageEdit != null && currentPage == 1) {
                        txMessageEdit.onEdit();
                    }

                } else if (baseTitle.getRightStr().equals("取消")) {
                    baseTitle.setRightStr("编辑");
                    bottom_ll.setVisibility(View.GONE);
                    quanxuan_check.setChecked(false);
                    if (messageEdit != null && currentPage == 0) {
                        messageEdit.onCancleEdit();
                    }

                    if (messageEdit != null && currentPage == 1) {
                        txMessageEdit.onCancleEdit();
                    }
                }
            }
        });

        quanxuan_ll.setOnClickListener(this);
        yidu_ll.setOnClickListener(this);
        delete_ll.setOnClickListener(this);

        quanxuan_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (currentPage == 0) {
                        messageEdit.onCheckAll();
                    } else if (currentPage == 1) {
                        txMessageEdit.onCheckAll();
                    }
                } else {
                    if (currentPage == 0) {
                        messageEdit.onCancleCheckAll();
                    } else if (currentPage == 1) {
                        txMessageEdit.onCancleCheckAll();
                    }
                }
            }
        });

    }

    private void initViewPager() {

        titles.add("公告");
        titles.add("提醒");

        fragments.add(new MessageCenterGGFragment());
        fragments.add(new MessageCenterTXFragment());


        FragmentAdapter adapter = new FragmentAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(this);
        XTabLayout tabLayout = (XTabLayout) findViewById(R.id.xTablayout);
        tabLayout.setupWithViewPager(viewPager);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        // Log.e("Message","OnPageScrolled当前的fragment的positin=="+position);
        currentPage = position;

    }

    @Override
    public void onPageSelected(int position) {
        //int current = viewPager.getCurrentItem();
        //Log.e("Message", "当前的fragment的positin==" + position);

        if (position == 1) {
            baseTitle.setRightStr("编辑");
            bottom_ll.setVisibility(View.GONE);
            quanxuan_check.setChecked(false);
        }
        if (position == 0) {
            baseTitle.setRightStr("编辑");
            bottom_ll.setVisibility(View.GONE);
            quanxuan_check.setChecked(false);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    MessageEdit messageEdit;

    public void setOnMessageEdit(MessageEdit messageEdit) {

        this.messageEdit = messageEdit;
    }

    TXMessageEdit txMessageEdit;

    public void setOnTXMessageEdit(TXMessageEdit txMessageEdit) {

        this.txMessageEdit = txMessageEdit;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_yidu_ll:
                //设为已读

                if (currentPage == 0) {
                    messageEdit.onRead();
                } else if (currentPage == 1) {
                    txMessageEdit.onRead();
                }
                quanxuan_check.setChecked(false);

                break;
            case R.id.id_delete_ll:
                //删除

                if (currentPage == 0) {
                    messageEdit.onDelete();
                } else if (currentPage == 1) {
                    txMessageEdit.onDelete();
                }
                quanxuan_check.setChecked(false);

                break;
        }
    }

}
